package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuImage;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.bean.SkuAllSaleValue;
import com.atguigu.gmall.product.mapper.BaseCategoryViewMapper;
import com.atguigu.gmall.product.mapper.SkuImageMapper;
import com.atguigu.gmall.product.mapper.SkuInfoMapper;
import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.atguigu.gmall.product.service.ApiProductService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApiProductServiceImpl implements ApiProductService {

    @Autowired
    SkuInfoMapper skuInfoMapper;

    @Autowired
    SkuImageMapper skuImageMapper;

    @Autowired
    BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    SpuSaleAttrMapper spuSaleAttrMapper;

    @Override
    public SkuInfo getSkuInfo(Long skuId) {

        //1、查询sku的详情
        SkuInfo skuInfo = skuInfoMapper.selectById(skuId);

        //2、查询sku的图片
        QueryWrapper<SkuImage> wrapper = new QueryWrapper<>();
        wrapper.eq("sku_id",skuId);
        List<SkuImage> skuImages = skuImageMapper.selectList(wrapper);

        //把查出的图片设置到bean属性
        skuInfo.setSkuImageList(skuImages);
        return skuInfo;
    }

    /**
     * 根据三级分类id，获取整个分类信息
     * @param category3Id
     * @return
     */
    @Override
    public BaseCategoryView getSkuCategorys(Long category3Id) {

        return baseCategoryViewMapper.selectById(category3Id);
    }

    /**
     * 根据skuid获取价格
     * @param skuId
     * @return
     */
    @Override
    public BigDecimal getSkuPrice(Long skuId) {

       BigDecimal price = skuInfoMapper.getSkuPrice(skuId);

        return price;
    }

    @Override
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(Long skuId, Long spuId) {
        return spuSaleAttrMapper.getSpuSaleAttrWithValueAndSkuCheck(skuId);
    }

    @Override
    public Map getSkuValueIdsMap(Long spuId) {

        List<SkuAllSaleValue> skuAllSaleValue = spuSaleAttrMapper.getSkuAllSaleValue(spuId);
        HashMap<String, String> map = new HashMap<>();
        if (!CollectionUtils.isEmpty(skuAllSaleValue)){
            for (SkuAllSaleValue saleValue : skuAllSaleValue) {
                map.put(saleValue.getValueIds(),saleValue.getSkuId());

            }
        }
        return map;
    }
}
